import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import type { SupportedLocale } from '@/locales'
import { setStoredLocale, SUPPORTED_LOCALES } from '@/locales'

export const useLocaleStore = defineStore('locale', () => {
  const { locale } = useI18n()
  
  // 当前语言
  const currentLocale = ref<SupportedLocale>(locale.value as SupportedLocale)
  
  // 切换语言
  const setLocale = (newLocale: SupportedLocale) => {
    if (SUPPORTED_LOCALES.includes(newLocale)) {
      currentLocale.value = newLocale
      locale.value = newLocale
      setStoredLocale(newLocale)
      
      // 触发页面元信息更新将由usePageTitle自动处理
      console.log('[Locale Store] Language switched to:', newLocale)
    }
  }
  
  // 获取当前语言
  const getCurrentLocale = () => currentLocale.value
  
  // 是否为中文
  const isChinese = () => currentLocale.value === 'zh-CN'
  
  // 是否为英文
  const isEnglish = () => currentLocale.value === 'en-US'
  
  return {
    currentLocale,
    setLocale,
    getCurrentLocale,
    isChinese,
    isEnglish
  }
}) 